﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using App.Utilities.Web.Handlers;
using CRMBiz;
using Newtonsoft.Json;
using System.Data;
using CRMWeb.Utilities.Context;
namespace CRMWeb.WebServices
{
    public class DualTemplate : BaseHandler
    {
        
        public override void OnMethodInvoke(OnMethodInvokeArgs e)
        {

            context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            context.Response.Cache.SetExpires(DateTime.MinValue);
            context.Response.Cache.SetNoStore();
            SetResponseContentType(ResponseContentTypes.HTML);
        }

        public override void AfterMethodInvoke(object result)
        {
            base.AfterMethodInvoke(result);
        }


        public void GetDualRoleTemplate(string Company)
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(DualTemplateHandler.GetCategoryRoleNodesList(Company));
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void GetAllDualTemplate(string Company)
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(DualTemplateHandler.GetCategoryNodesList(Company));
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void GetDualTemplateContentById(int DualTemplateId)
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(DualTemplateHandler.GetDualTemplates(DualTemplateId));
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void DisplayDualTemplateNode(string Company)
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(DualTemplateHandler.GetCategoryNodesList(Company));
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }
        public void DisplayDualCategoryList(string Company)
        {
            try
            {
                bool isSMS = true;
                string strJson = JsonConvert.SerializeObject(CategoryNodeHandler.GetTemplateCategoryNodesList(isSMS, true, true, Company));
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }
        //public void GetAllDualTemplate1()
        //{
        //    try
        //    {
        //        string strJson = JsonConvert.SerializeObject(DualTemplateHandler.GetDualTemplates1());
        //        context.Response.Write(strJson);
        //    }
        //    catch (Exception ex)
        //    {
        //        ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
        //    }
        //}

        public int saveDualTemplate(string DualTemplateID, string DualSubject, string DualTemplate, string SmsTemplateID, string EmailTemplateID, int CategoryID, string DualCompany)
        {
            int intReturnValue = 0;
            try
            {
               intReturnValue= DualTemplateHandler.saveDualTemplate(Convert.ToInt32(DualTemplateID), DualSubject, DualTemplate, Convert.ToInt32(SmsTemplateID), Convert.ToInt32(EmailTemplateID), CategoryID, DualCompany);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }

        public void deleteDualTemplate(int DualTemplateID)
        {
            try
            {
                DualTemplateHandler.deleteDualTemplate(DualTemplateID);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        //Display email data list:
        public void GetDualTemplateList(string Company,int CategoryId)
        {
            try
            {
                var pageIndex = string.IsNullOrEmpty(context.Request["page"]) ? 1 : int.Parse(context.Request["page"]);
                var pageSize = string.IsNullOrEmpty(context.Request["rows"]) ? 10 : int.Parse(context.Request["rows"]);
                DataSet ds = DualTemplateHandler.GetDualTemplateList(pageIndex, pageSize,Company,CategoryId );
                var total = (ds.Tables[0].Rows.Count > 0) ? Convert.ToInt32(ds.Tables[0].Rows[0]["Total"]) : 0;
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                strJson = "{\"total\":\"" + total.ToString() + "\",\"rows\":" + strJson + "}";

                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        //Delete list data:
        public bool DeleteDualListData(int DualTemplateID)
        {
            bool flag = false;
            try
            {
                flag = DualTemplateHandler.DeleteDualListData(DualTemplateID);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return flag;
        }

    }
}